package com.lw.extraceim.ui.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lw.extraceim.R;
import com.lw.extraceim.model.CustomerInfo;
import com.lw.extraceim.model.ExpressSheet;

import java.util.List;

/**
 * @Author : liwei
 * @Email : 1759840027@qq.com
 * @Description : 快件信息列表的适配器
 * @Date : 2019/4/16 9:35
 */
public class ExpressSheetAdapter extends BaseAdapter {

    private static final String TAG = "ExpressSheetAdapter";
    private List<ExpressSheet> expressSheetList;
    private Context mContext;

    public ExpressSheetAdapter(List<ExpressSheet> expressSheetList, Context context) {
        this.expressSheetList = expressSheetList;
        this.mContext = context;
    }

    @Override
    public int getCount() {
        if (expressSheetList != null) {
            return expressSheetList.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.exptess_sheet_item, null);
            holder = new ViewHolder();
            holder.mIvExpressSheetImg  = convertView.findViewById(R.id.iv_express_sheet_img);
            holder.mTvExpressSheetId = convertView.findViewById(R.id.tv_express_sheet_id);
            holder.mTvExpressSheetStatus = convertView.findViewById(R.id.tv_express_sheet_status);
            holder.mTvExpressSheetSendLocation = convertView.findViewById(R.id.tv_express_sheet_send_location);
            holder.mTvExpressSheetReciveLocation = convertView.findViewById(R.id.tv_express_sheet_recive_location);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        ExpressSheet es = expressSheetList.get(position); //获取快件信息
        if(es != null){
//            holder.mIvExpressSheetImg;
//            Log.d(TAG, "getView: " + es);
            holder.mTvExpressSheetId.setText(es.getId());
            holder.mTvExpressSheetStatus.setText(es.getStatusString());
            CustomerInfo sender = es.getSender();
            CustomerInfo revicver = es.getRecever();
            if(sender != null) {
                holder.mTvExpressSheetSendLocation.setText(sender.getRegionString() + " " + sender.getAddress());
            }
            if(revicver != null) {
                holder.mTvExpressSheetReciveLocation.setText(revicver.getRegionString() + " " + revicver.getAddress());
            }
        }
        return convertView;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    public void setData(List<ExpressSheet> expressSheetList){
        this.expressSheetList = expressSheetList;
        notifyDataSetChanged();
    }
    public List<ExpressSheet> getData(){
        return this.expressSheetList;
    }
    class ViewHolder {
        ImageView mIvExpressSheetImg;
        TextView mTvExpressSheetId;
        TextView mTvExpressSheetStatus;
        TextView mTvExpressSheetSendLocation;
        TextView mTvExpressSheetReciveLocation;
    }
}
